/*
 * Producer.h
 *
 *  Created on: Feb 6, 2010
 *      Author: Jamie LaPointe
 */

#ifndef PRODUCER_H_
#define PRODUCER_H_

#include "ProducerConsumerShared.h"

namespace ProducerConsumer
{

namespace Producer
{

class Producer
{
private:
   Common::ProducerConsumerShared * producerConsumerShared_;
public:
   Producer(Common::ProducerConsumerShared * producerConsumerShared)
   {
      producerConsumerShared_ = producerConsumerShared;
   }

   void run(Common::Request& request)
   {
      pthread_mutex_lock(&producerConsumerShared_->mutex);

      while (producerConsumerShared_->requestList.size()
            == producerConsumerShared_->requestList.capacity())
      {
         pthread_cond_wait(&producerConsumerShared_->producerCond,
               &producerConsumerShared_->mutex);
      }

      producerConsumerShared_->requestList.push_back(request);

      pthread_mutex_unlock(&producerConsumerShared_->mutex);
      pthread_cond_signal(&producerConsumerShared_->consumerCond);
   }
}; // class Producer

} // namespace Producer

} // namespace ProducerConsumer

#endif /* PRODUCER_H_ */
